package com.jinxin.platform.cdfaceacs.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdfaceacs.bean.CdzxkacsDevice;
import com.jinxin.platform.cdfaceacs.mapper.CdzxkacsDeviceMapper;
import com.jinxin.platform.cdfaceacs.service.IAcsDeviceService;
import com.jinxin.platform.cdfaceacs.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-01-28
 * @Author: Lingnan
 **/
@Service
public class AcsDeviceServiceImpl implements IAcsDeviceService {
    @Autowired
    private CdzxkacsDeviceMapper deviceMapper;

    @Override
    public List<CdzxkacsDevice> list(HashMap param) {
        return deviceMapper.list(param);
    }

    @Override
    public List<CdzxkacsDevice> listPage(HashMap param) {
        return deviceMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return deviceMapper.listPageCount(param);
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("资源不存在");
        }
        int result = deviceMapper.deleteByKey(id);
        if(result > 0){
            return JsonResult.ok("删除成功","");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult saveOrUpdate(CdzxkacsDevice deviceInfo) {
        if (deviceInfo == null){
            return JsonResult.error("参数错误，操作失败");
        }
        String id = deviceInfo.getId();
        int count;
        String msg = "";
        if(StringUtils.isEmpty(id)){
            deviceInfo.setCreateTime(LocalDateTime.now());
            deviceInfo.setId(StringUtil.getUuid());
            count = deviceMapper.insert(deviceInfo);
            msg = "新增成功";
        }else {
            count = deviceMapper.updateByKey(deviceInfo);
            msg = "更新成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,"");
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }

    @Override
    public JsonResult countDeviceStatus(String communityId) {
        HashMap param = new HashMap();
        param.put("communityId",communityId);
        int count = deviceMapper.listPageCount(param);
        param.put("status",1);
        int count1 = deviceMapper.listPageCount(param);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("onlineCount",count1);
        resultMap.put("offlineCount",(count-count1));
        return JsonResult.ok("success",resultMap);
    }
}
